package com.maven.front.controller.manageCourse;

import com.maven.front.bean.EduCourse;
import com.maven.front.bean.FileUploadResult;
import com.maven.front.bean.Result;
import com.maven.front.dto.EduCourseDto;
import com.maven.front.service.manageCourse.EduCourseService;
import com.maven.front.service.manageSubject.SysSubjectService;
import com.maven.front.service.teacher.EduTeacherService;
import com.maven.front.util.FileUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
//    课程管理模块：
//

@Slf4j
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("admin/eduCourse")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;
    @Autowired
    private SysSubjectService sysSubjectService;
    @Autowired
    private FileUploadService fileUploadService;


    //    查询所有或者条件查询
    @PostMapping("AllEduCourse")
    public Result listAll(EduCourse eduCourse) throws Exception{
        Integer c = eduCourse.getSubjectLink();
        log.info("专业父节点："+c);
        if(eduCourse.getAddTime()!=null){
            eduCourse.setAddTime(toDate(eduCourse.getAddTime()));
        }
        if(eduCourse.getEndTime()!=null){
            eduCourse.setEndTime(toDate(eduCourse.getEndTime()));
        }
        List<EduCourseDto> list = eduCourseService.listAll(eduCourse);
        if (CollectionUtils.isEmpty(list)){
            return new Result(10005, "查询失败,数据为空！", null);
        }else{
            return new Result(200, "查询成功", list);
        }
    }


    //    创建课程时，选择教师跳转至教师页面( 查询所有教师以及模糊查询页面 )
    @GetMapping("AllTeacher")
    public Result AllTeacher(Integer id, String name){
        return eduCourseService.selAllTeachaer(id ,name);
    }
    //    选择教师完毕，将选中的教师名称显示到前台
    @GetMapping("getTeacherName")
    public Result getTeacherName(String tid){
        return eduCourseService.getTeacher(tid);
    }


    //    新增课程之前，上传课程图片
    @ResponseBody
    @PostMapping("saveImg")
    public Result svaeImg(@RequestParam("file")MultipartFile file){
        FileUploadResult fileUploadResult = fileUploadService.upload(file);
        return new Result(200, "成功！", fileUploadResult);
    }

    //    新增课程
    @PostMapping("saveEduCourse")
    public Result saveEduCourse(EduCourseDto eduCourse){
        return eduCourseService.saveEduCourse(eduCourse);
    }


    //    修改数据之前对要修改的数据进行查询
    @GetMapping("getById")
    public Result getById(Integer courseId){
        log.info("课程ID："+courseId);
        List<Object> list = eduCourseService.getAllCourse(courseId);
        if (CollectionUtils.isEmpty(list)){
            return new Result(10005, "失败！", null);
        } else {
            return new Result(200, "成功", list);
        }
    }
//    跳转至修改页面前，先获取教师信息
    @GetMapping("editGetTeacher")
    public Result editGetTeacher(Integer courseId){
        return eduCourseService.editGetTeacher(courseId);
    }
    //    修改数据
    @PostMapping("updateEduCourse")
    public Result updateEduCourse(EduCourseDto eduCourseDto)throws Exception{
        return eduCourseService.upCourse(eduCourseDto);
    }



    //      删除课程，做逻辑删除，修改状态
    @PostMapping("deleteCourse")
    public Result deleteCourse(@RequestBody LinkedHashMap<String,String> params){
        String courseId = params.get("courseId");
        return eduCourseService.deleteCourseId(Integer.valueOf(courseId));
    }

    //      对课程进行章节管理操作------------------暂时无用------错查
    @GetMapping("selectByPrimaryKey")
    public Result selectByPrimaryKey(String courseId){
        return sysSubjectService.selectByPrimaryKey(Integer.valueOf(courseId));
    }



//    日期格式转换
    public Date toDate(Date date) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strdate = sdf.format(date);
        Date adddate = sdf.parse(strdate);
        return adddate;
    }

}